package com.anolesoft.PlanOrder.controller;

import com.anolesoft.PlanOrder.dto.MesBzzFcfhjhDto;
import com.anolesoft.PlanOrder.service.FengCunFangHuService;
import com.anolesoft.PlanOrder.util.BarCodeGenerate;
import com.anolesoft.PlanOrder.vo.MesBzzFcfhjhVo;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/planOrder/FengCunFanghu")
public class FengCungFangHuController {

    @Autowired
    FengCunFangHuService fengCunFangHuService;
    @Autowired
    BarCodeGenerate barCodeGenerate;

    //封存防护计划查询
    @GetMapping(value="/query",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryMesGjgyxxFkszInfo(MesBzzFcfhjhDto mesBzzFcfhjhDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesBzzFcfhjhDto.setFactory(loginUser.getFacCode());
        List<MesBzzFcfhjhVo> allList = fengCunFangHuService.selectListByCondition(mesBzzFcfhjhDto);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  mesBzzFcfhjhDto.getPageNo().intValue();
        int ps = mesBzzFcfhjhDto.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesBzzFcfhjhVo> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, mesBzzFcfhjhDto.getPageSize(), mesBzzFcfhjhDto.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }

    //维护
    @PostMapping(value="/weiHu", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult weiHuMesGjgyxxFkszInfoDto(MesBzzFcfhjhDto mesBzzFcfhjhDto){
//        System.out.println(mesBzzFcfhjhDto.toString());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesBzzFcfhjhDto.setWeihuren(loginUser.getUserUid());
        mesBzzFcfhjhDto.setFactory(loginUser.getFacCode());
        return new AjaxResult(HttpStatus.SUCCESS, "维护成功!", fengCunFangHuService.weiHu(mesBzzFcfhjhDto));
    }

    //取货
    @PostMapping(value="/quHuo", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult quHuoMesGjgyxxFkszInfoDto(MesBzzFcfhjhDto mesBzzFcfhjhDto){
//        System.out.println(mesBzzFcfhjhDto.toString());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesBzzFcfhjhDto.setQuhuoren(loginUser.getUserUid());
        return new AjaxResult(HttpStatus.SUCCESS, "取货成功!", fengCunFangHuService.quHuo(mesBzzFcfhjhDto));
    }

    //出库
    @PostMapping(value="/chuKu", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult chuKuMesGjgyxxFkszInfoDto(MesBzzFcfhjhDto mesBzzFcfhjhDto){
//        System.out.println(mesBzzFcfhjhDto.toString());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesBzzFcfhjhDto.setWanchengren(loginUser.getUserUid());
        return new AjaxResult(HttpStatus.SUCCESS, "出库确认成功!", fengCunFangHuService.chuKu(mesBzzFcfhjhDto));
    }

    //完检
    @PostMapping(value="/wanJian", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult wanJianMesGjgyxxFkszInfoDto(MesBzzFcfhjhDto mesBzzFcfhjhDto){
//        System.out.println(mesBzzFcfhjhDto.toString());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesBzzFcfhjhDto.setWanjianren(loginUser.getUserUid());
        return new AjaxResult(HttpStatus.SUCCESS, "计量期完检入库成功!", fengCunFangHuService.wanJian(mesBzzFcfhjhDto));
    }

    //入库
    @PostMapping(value="/ruKu", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult ruKuMesGjgyxxFkszInfoDto(MesBzzFcfhjhDto mesBzzFcfhjhDto){
//        System.out.println(mesBzzFcfhjhDto.toString());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        mesBzzFcfhjhDto.setRukuren(loginUser.getUserUid());
        return new AjaxResult(HttpStatus.SUCCESS, "库房入库成功!", fengCunFangHuService.ruKu(mesBzzFcfhjhDto));
    }

    //查询派工人员姓名-胸卡号
    @GetMapping(value="/queryEidName",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryPaiGongEidName(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        MesBzzFcfhjhDto build = MesBzzFcfhjhDto.builder().build();
        build.setFactory(loginUser.getFacCode());
        List<MesBzzFcfhjhVo> allList = fengCunFangHuService.selectEidName(build);
        AjaxResult success = AjaxResult.success();
        success.put("data", allList);
        return success;
    }

    //派工  __不内部封存出库,无法点击派工
    @PostMapping(value="/paiGong", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult paiGongMesGjgyxxFkszInfoDto(MesBzzFcfhjhDto mesBzzFcfhjhDto){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String uuid = UUID.randomUUID().toString();
        uuid= uuid.replace("-","");
        mesBzzFcfhjhDto.setObjuid(uuid);
        mesBzzFcfhjhDto.setBarcodeobjuid(uuid);
        mesBzzFcfhjhDto.setBarcode(barCodeGenerate.getBarCoede(loginUser.getFacCode()));
        return new AjaxResult(HttpStatus.SUCCESS, "派工成功!", fengCunFangHuService.paiGong(mesBzzFcfhjhDto));
    }
}
